import {
	getTypeNameAndRouter
} from '@/api/system/form_module'
import {
  formatDate
} from '@/utils/util'

const state = {
	get_date: null,
	flow_obj: null
}

const mutations = {
	SET_GET_DATE: (state, get_date) => {
		state.get_date = get_date
	},
	SET_FLOW_OBJ: (state, flow_obj) => {
		state.flow_obj = flow_obj
	}
}

const actions = {
	getFlowObj({
		commit
	}) {
		return new Promise((resolve, reject) => {
			var needQuery = true;
			var now = formatDate(new Date())
			if(!!state.get_date){
				needQuery = (state.get_date != now);
			}
			console.log(state);
			// 一天只拉取一次
			if(!!needQuery){
				commit('SET_GET_DATE', now)
				getTypeNameAndRouter()
					.then(response => {
						const {
							data
						} = response;
						commit('SET_FLOW_OBJ', data);
						resolve();
					}).catch(error => {
						reject(error);
					})
			}
			
			
		})
	},
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
